package com.jianjian.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jianjian.pojo.Brand;
import com.jianjian.pojo.PageBean;
import com.jianjian.service.BrandService;
import com.jianjian.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Zsir
 * @create 2021-12-06 21:30
 */
@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandService bs = new BrandServiceImpl();

    //分页查询
    //查询所有数据的总数
    public void selectAllPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        PageBean<Brand> brandPageBean = bs.selectAllPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));

        String jsonString = JSON.toJSONString(brandPageBean);

        resp.setContentType("text/json;charSet=utf-8");
        resp.getWriter().write(jsonString);

    }

    //分页条件查询
    //查询所有数据的总数
    public void selectTPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        BufferedReader reader = req.getReader();
        String brandss = reader.readLine();

        Brand brands = JSON.parseObject(brandss,Brand.class);
        System.out.println(brands);
        PageBean<Brand> pageBean = bs.selectTPage(currentPage, pageSize, brands);
        System.out.println(pageBean);
        String jsonString = JSON.toJSONString(pageBean);

        resp.setContentType("text/json;charSet=utf-8");
        resp.getWriter().write(jsonString);


    }

    //删除一条数据
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        BufferedReader reader = req.getReader();
        String id = reader.readLine();

        bs.deleteById(Integer.parseInt(id));

        resp.getWriter().write("success");

    }


    //修改多条数据
    public void deleteByIdAllBrand(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        BufferedReader reader = req.getReader();
        String readLine = reader.readLine();

        int[] id = JSON.parseObject(readLine, int [].class);

        bs.deleteByIdAllBrand(id);

        resp.getWriter().write("success");

    }


    //修改一条数据
    public void updateBrand(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         req.setCharacterEncoding("utf-8");
        BufferedReader reader = req.getReader();
        String readLine = reader.readLine();

        Brand brand = JSON.parseObject(readLine, Brand.class);

        bs.updateBrand(brand);

        resp.getWriter().write("success");

    }

    //添加一条数据
    public void addBrand(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         req.setCharacterEncoding("utf-8");
        BufferedReader reader = req.getReader();
        String readLine = reader.readLine();

        Brand brand = JSON.parseObject(readLine, Brand.class);
        bs.addBrand(brand);

        resp.getWriter().write("success");

    }
    //查询所有数据
    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          req.setCharacterEncoding("utf-8");
        List<Brand> brands = bs.selectAllBrand();
        String jsonString = JSON.toJSONString(brands);

        resp.setContentType("text/json;charSet=utf-8");
        resp.getWriter().write(jsonString);
    }


}
